package com.igeek.collateral.config;

import lombok.extern.slf4j.Slf4j;
import org.flowable.app.spring.SpringAppEngineConfiguration;
import org.flowable.spring.boot.EngineConfigurationConfigurer;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.*;
import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import javax.sql.DataSource;
import java.beans.PropertyVetoException;

/**
 * TODO
 *
 * @author chh
 * @since 2024/7/19
 **/
@Configuration
@PropertySource("classpath:application.yml")
@Slf4j
public class FlowableConfig implements EngineConfigurationConfigurer<SpringAppEngineConfiguration> {

    //读取配置
    @Value("${flow.username}")
    private String user;
    @Value("${flow.password}")
    String password;
    @Value("${flow.url}")
    String jdbcUrl;
    @Value("${flow.driver-class-name}")
    String driverClass;
//    @Value("${flow.initialPoolSize}")
//    Integer initialPoolSize;
    @Value("${flow.maxPoolSize}")
    int maxPoolSize;

    //配置数据源
    @Bean
    public DruidDataSource flowDataSource() throws PropertyVetoException {
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setUsername(user);
        dataSource.setPassword(password);
        dataSource.setUrl(jdbcUrl);
        dataSource.setDriverClassName(driverClass);
        //dataSource.setInitialSize(initialPoolSize);
        dataSource.setMaxPoolPreparedStatementPerConnectionSize(maxPoolSize);
        return dataSource;
    }

    /**
     * @return
     * @throws PropertyVetoException
     */
    @Bean
    @Primary
    @ConfigurationProperties(prefix = "spring.datasource.druid")
    public DruidDataSource dataSource() throws PropertyVetoException {
        return new DruidDataSource();
    }

    //配置第一个数据源的事务管理器，定义bean名称为 transactionManagerOne
    @Primary
    @Bean(name = "transactionManagerOne")
    public PlatformTransactionManager transactionManagerOne(@Qualifier("dataSource") DataSource dataSourceOne) {
        return new DataSourceTransactionManager(dataSourceOne);
    }

    //flowable 事物管理器
    @Bean(name = "transactionManagerTwo")
    public PlatformTransactionManager transactionManagerTwo() throws PropertyVetoException {
        return new DataSourceTransactionManager(flowDataSource());
    }

    @Override
    public void configure(SpringAppEngineConfiguration engineConfiguration) {
        try {
            //把数据源设置进来
            engineConfiguration.setDataSource(flowDataSource());
            //配置对应的事务管理器
            engineConfiguration.setTransactionManager(transactionManagerTwo());
            log.info("配置flowable数据源成功");
        } catch (PropertyVetoException e) {
            throw new RuntimeException(e);
        }
    }
}
